package com.robam.gulimall.product.service.impl;

import com.robam.common.exception.BizCodeEnum;
import com.robam.common.exception.GulimallException;
import com.robam.gulimall.product.entity.BrandEntity;
import com.robam.gulimall.product.entity.CategoryEntity;
import com.robam.gulimall.product.service.BrandService;
import com.robam.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.Query;

import com.robam.gulimall.product.dao.CategoryBrandRelationDao;
import com.robam.gulimall.product.entity.CategoryBrandRelationEntity;
import com.robam.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.util.StringUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存时前端传过来的数据校验
     * 1.不能重复报错
     * 2.品牌id和分类id须存在
     * @param categoryBrandRelation
     * @return
     */
    @Override
    public boolean saveValidate(CategoryBrandRelationEntity categoryBrandRelation) {
        // 品牌id和分类id不能为空
        if(StringUtils.isEmpty(categoryBrandRelation.getBrandId()) ||
            StringUtils.isEmpty(categoryBrandRelation.getCatelogId())) {
            throw new GulimallException(BizCodeEnum.BrandOrCategoryIsEmpty.getCode(),BizCodeEnum.BrandOrCategoryIsEmpty.getMsg());
        }
        // 品牌id和分类id存在
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if(brandEntity == null || categoryEntity == null) {
            throw new GulimallException(BizCodeEnum.BrandOrCategoryNotExist.getCode(),BizCodeEnum.BrandOrCategoryNotExist.getMsg());
        }
        // 数据库不存在此关联记录
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id",categoryBrandRelation.getBrandId()).eq("catelog_id", categoryBrandRelation.getCatelogId());
        if(this.baseMapper.selectCount(wrapper)>0) {
            throw new GulimallException(BizCodeEnum.BrandCategoryRelationExist.getCode(),BizCodeEnum.BrandCategoryRelationExist.getMsg());
        }
        // 获取相应的名称
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        return true;
    }

}